from django.shortcuts import render
from django.http import JsonResponse
from django.db.models import Count
from web import models
import collections, datetime, time


def dashboard(request, project_id):
    """
    项目概览
    :param request:
    :param project_id:
    :return:
    """
    # 问题数据处理
    status_dict = collections.OrderedDict()
    for key, text in models.Issues.status_choices:
        status_dict[key] = {'text': text, 'count': 0}

    issues_data = models.Issues.objects.filter(project_id=project_id).values('status').annotate(ct=Count('id'))
    for item in issues_data:
        status_dict[item['status']]['count'] = item['ct']

    # 项目成员
    user_list = models.ProjectUser.objects.filter(project_id=project_id).values('user_id', 'user__username')

    # 最近的10个问题
    top_ten = models.Issues.objects.filter(project_id=project_id, assign__isnull=False).order_by('-id')[0:10]

    context = {
        'status_dict': status_dict,
        'user_list': user_list,
        'top_ten_object': top_ten
    }
    return render(request, 'dashboard.html', context)


def issues_chart(request, project_id):
    """ 在概览页面生成highcharts所需的数据 """
    today = datetime.datetime.now().date()
    date_dict = collections.OrderedDict()
    for i in range(0, 30):
        date = today - datetime.timedelta(days=i)
        date_dict[date.strftime("%Y-%m-%d")] = [time.mktime(date.timetuple()) * 1000, 0]
    # [('2020-04-06': [1586102400000.0, 0]), ('2020-04-05': [1586016000000.0, 0])]

    # select xxxx,1 as ctime from xxxx
    # select id,name,email from table;
    # select id,name, strftime("%Y-%m-%d",create_datetime) as ctime from table;

    # sqlite 格式化时间样式的语法
    # "strftime('%%Y-%%m-%%d',web_issues.create_datetime)"
    result = models.Issues.objects.filter(project_id=project_id,
                                          create_datetime__gte=today - datetime.timedelta(days=30)).extra(
        select={'ctime': "DATE_FORMAT(web_issues.create_datetime,'%%Y-%%m-%%d')"}).values('ctime').annotate(
        ct=Count('id'))
    # [{'ctime': '2020-03-31', 'ct': 1}, {'ctime': '2020-04-05', 'ct': 1}]

    for item in result:
        date_dict[item['ctime']][1] = item['ct']

    print(date_dict)

    return JsonResponse({'status': True, 'data': list(date_dict.values())})
